package cn.jbooter.coder.helper;

import java.util.Arrays;
import java.util.List;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.converters.IParameterSplitter;

import cn.jbooter.coder.model.CmdLine;
import cn.jbooter.coder.util.JdbcUtil;
import cn.jbooter.coder.util.StringUtil;


/**
 * 命令行帮组类
 * @author hejian
 *
 */
public class CmdHelper {

	/**
	 * 逗号参数分割器
	 * @author hejian
	 *
	 */
	public static class CommaParameterSplitter implements IParameterSplitter{
		@Override
		public List<String> split(String value) {
			return Arrays.asList(value.split(","));
		}
	}
	
	/**
	 * 命令行参数解析
	 * @param commanderLine 解析到的命令行参数模型
	 * @param args 命令行参数
	 */
	public static void parseArgument(CmdLine cmdline, String... args) {
	    JCommander jcmdr = new JCommander(cmdline, args);
	    jcmdr.setProgramName("jbooter-coder.");
	    if(cmdline.isHelp()) {
	    	StringBuilder sb = new StringBuilder();
	    	sb.append("jbooter-coder v0.0.1 \r\nAuthor:hejian \r\n");
	    	jcmdr.usage(sb);
	    	jcmdr.getConsole().println(sb.toString());
	    }
	    if(!StringUtil.isEmpty(cmdline.getUrl())) {
	    	//解析jdbc driver
	    	cmdline.setDriver(JdbcUtil.getDriverClassName(cmdline.getUrl()));
	    	cmdline.setDbType(JdbcUtil.getDbType(cmdline.getUrl()));
	    }
	}
	
	public static void main(String[] args) {
		args = new String[] {
				"--url", "jdbc:oracle:thin:", 
				"-u", "你好",
				"-p", "123", 
				"-o", "~/others/",
				"-f", "com.project",
				"-s", "system.manage",
				"-m", "service,dao",
				"-t", "a_sys_menu,a_sys_user",
				"--help",
		};

		CmdLine m = new CmdLine();
		parseArgument(m,args);
		System.out.println(m);
	}
	
}
